#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 7;
int N, M;
char A[maxn], B[maxn];
int dp[maxn][maxn];
int main() {
  cin >> N >> M >> A + 1 >> B + 1;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      if (A[i] == B[j]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i][j - 1]});
    }
  }
  cout << dp[N][M] << endl;
}
